﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace WebCumpleanios
{
    public partial class saveImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //Get the stream
                string id = Page.Request.Params["id"];
                string nombreimagenActual = Page.Request.Params["ES"];
                string nuevoNombreImagen = id + "_" + Guid.NewGuid().ToString() + "_" + DateTime.Now.Year;

                Stream input = (Stream)Request.InputStream;
                Bitmap bmp = new Bitmap(input);
                Response.Clear();
                Response.ContentType = "image/jpeg";
                bmp.Save(Server.MapPath("./fla/imagenes/") + nuevoNombreImagen + ".jpg", ImageFormat.Jpeg);
                bmp.Dispose();

                /**/
                SqlConnection _cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnx1"].ToString());
                SqlCommand _cmd = new SqlCommand();
                _cmd.CommandType = CommandType.StoredProcedure;
                _cmd.Connection = _cnn;
                _cmd.CommandText = "proc_newNombreImagen";
                _cmd.Parameters.Add(new SqlParameter("@id", id));
                _cmd.Parameters.Add(new SqlParameter("@imagen", nuevoNombreImagen));
                _cnn.Open();
                _cmd.ExecuteNonQuery();
                _cnn.Close();

                if (File.Exists(Server.MapPath(@"\appCumpleanio\fla\imagenes\"+ nombreimagenActual+".jpg")))
                {
                    File.Delete(Server.MapPath(@"\appCumpleanio\fla\imagenes\"+ nombreimagenActual+".jpg"));
                }
                
                /**/
                Response.End();
            }
            catch (Exception ex)
            {
                Utils.WriteEventLog(ex);
            }

        }
    }
}